package com.ebelter.btcomlib.models.bluetooth.products.ear_temperature;

import android.content.Context;
import android.text.TextUtils;
import com.ebelter.btcomlib.R;
import com.ebelter.btcomlib.models.bluetooth.base.BaseManager;
import com.ebelter.btcomlib.models.bluetooth.bean.ProductStyle;
import com.ebelter.btcomlib.models.bluetooth.interfaces.IDataReadWriteCallback;
import com.ebelter.btcomlib.models.bluetooth.products.ear_temperature.interfaces.EarTemperatureMesureResult;
import com.ebelter.btcomlib.utils.BytesUtils;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.SpUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;

import static com.ebelter.btcomlib.bases.activity.BaseActivity.getString_;

/**
 * 耳温枪
 */
public class EarTemperatureManager extends BaseManager{

    private static final String TAG = "EarTemperatureManager";



    private EarTemperatureBytesAnalysis mEarTemperatureBytesAnalysis;
    private EarTemperatureBytesMake mEarTemperatureBytesMake;




    public EarTemperatureManager(Context mContext) {
        super(mContext, ProductStyle.EAR_THERMOMETER, EarTemperatureConstant.EarTemperature_SERVICE_UUID, new String[]{EarTemperatureConstant.EarTemperature_WRITE_UUID},
                new String[]{EarTemperatureConstant.EarTemperature_READ_UUID1},true,500);

        mEarTemperatureBytesAnalysis = new EarTemperatureBytesAnalysis();
        mEarTemperatureBytesMake = new EarTemperatureBytesMake();


        mServiceGatt.setIDataReadWriteCallback(new IDataReadWriteCallback() {
            @Override
            public void onCharacteristicChanged(byte[] data) {
              //  LogUtils.i(TAG, "EarTemperatureManager--IDataReadWriteCallback-onCharacteristicChanged--读到-" + BytesUtils.bytes2HexStr(data));
                mEarTemperatureBytesAnalysis.resultAnalysis(data);
            }

            @Override
            public void writeData(byte[] data) {
                // LogUtils.i(TAG,"ScaleBlue--IDataReadWriteCallback-writeData--写入-"+ Arrays.toString(data));
            }
        });

    }







    public boolean isBondDevice() {
        String add = getBondDeviceAddress();
        return !TextUtils.isEmpty(add);
    }

    public String getBondDeviceAddress() {
        return SpUtil.readString(EarTemperatureConstant.EarTemperature_BOND_DEVICE_ADDRESS, null);
    }

    public void setBondDeviceAddress(String address) {
        SpUtil.writeString(EarTemperatureConstant.EarTemperature_BOND_DEVICE_ADDRESS, address);
    }




    public void setEarTemperatureMeasureCallback(EarTemperatureMesureResult earTemperatureMesureResult) {
        mEarTemperatureBytesAnalysis.setEarTemperatureMeasureCallback(earTemperatureMesureResult);
    }


    //发送一条接收成功的ack
    public void sendReceiveSuccessACK() {
        addSendMsg(mEarTemperatureBytesMake.getReceiveSuccessAck());
    }


    public static String getErrorCodeDesc(int errorCode){
        /**
         *0x01:测量结果温度低，LCD 显示 L
         0x02:测结果温度高，LCD 显示 H
         0x03:环境温度过低，LCD 显示 EL
         0x04:环境温度过高，LCD 显示 EH
         0x05:低电压，LCD 显示电池符号
         0x06:其他Err 测量错误，LCD 显示 Err
         */
        if (errorCode==1){
            return getString_(R.string.clwdgd);
        }else if(errorCode==2){
            return getString_(R.string.clwdgg);
        }else if(errorCode==3){
            return getString_(R.string.hjwdgd);
        }else if(errorCode==4){
            return getString_(R.string.hjwdgg);
        }else if(errorCode==5){
            return getString_(R.string.dld);
        }else if(errorCode==6){
            return getString_(R.string.clcw);
        }
        return "";
    }



    public static String getResultDesc(float temperature){
        if (temperature<36.0f){
            return getString_(R.string.guodi);
        }else if (temperature>=36.0f&&temperature<=37.3f){

            return getString_(R.string.zhengchang);
        }else if (temperature>=37.4f&&temperature<=38.0f){

            return getString_(R.string.dire);
        }else if (temperature>=38.1f&&temperature<=39.0f){

            return getString_(R.string.zhdfr);
        }else if (temperature>=39.1f&&temperature<=41.0f){

            return getString_(R.string.gaore);
        }else if (temperature>=41.1f){

            return getString_(R.string.chaogaore);
        }
        return "";
    }
    public static int getResultLevel(float temperature){
        temperature= NumUtils.numBaoLiuWei1(temperature);
        if (temperature<36.0f){
            return 0;
        }else if (temperature>=36.0f&&temperature<=37.3f){

            return 1;
        }else if (temperature>=37.4f&&temperature<=38.0f){

            return 2;
        }else if (temperature>=38.1f&&temperature<=39.0f){

            return 3;
        }else if (temperature>=39.1f&&temperature<=41.0f){

            return 4;
        }else if (temperature>=41.1f){

            return 5;
        }
        return 1;
    }
}
